package com.enterprise.util;

import org.apache.commons.lang.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddressResolutionUtil {

    /**
     * 解析地址
     * @author lin
     * @param address
     * @return
     */
    public static Map<String,String> addressResolution(String address){
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)" +
                "(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)" +
                "(?<district>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)";
        String regexb="(?<province>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)" +
                "(?<district>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)";
        Matcher m;
        Map<String,String> row=null;
        String province=null,city=null,district=null;
        if (address.startsWith("北京") || address.startsWith("上海") ||
                address.startsWith("重庆") || address.startsWith("天津") ){
            m=Pattern.compile(regexb).matcher(address);
            while(m.find()){
                row=new LinkedHashMap<String,String>();
                province=m.group("province");
                row.put("province", province==null?"":province.trim());
                district=m.group("district");
                row.put("district", district==null?"":district.trim());
            }
        }else {
            m=Pattern.compile(regex).matcher(address);
            while(m.find()){
                row=new LinkedHashMap<String,String>();
                province=m.group("province");
                row.put("province", province==null?"":province.trim());
                city=m.group("city");
                row.put("city", city==null?"":city.trim());
                district=m.group("district");
                row.put("district", district==null?"":district.trim());
            }
        }
        if (row == null){
            row = addressResolutionTwo(address);
        }
        return row;
    }

    public static Map<String,String> addressResolutionTwo(String address){
        String regexp="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)";
        String regexc="(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)";
        String regexd="(?<district>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)";
        Matcher m;
        Map<String,String> row=new LinkedHashMap<String,String>();
        String province=null,city=null,district=null;
        m=Pattern.compile(regexp).matcher(address);
        while(m.find()){
            province=m.group("province");
            row.put("province", province==null?"":province.trim());
        }
        m=Pattern.compile(regexc).matcher(StringUtils.substringAfter(address, province));
        while(m.find()){
            city=m.group("city");
            row.put("city", city==null?"":city.trim());
        }
        m=Pattern.compile(regexd).matcher(address);
        while(m.find()){
            district=m.group("district");
            row.put("district", district==null?"":district.trim());
        }

        return row;
    }

    public static void main(String[] args) {
        String str = "河南省扶沟县";
        Map<String, String> map = addressResolutionTwo(str);
        String province = map.get("province");
        String s = StringUtils.substringAfter(str, province);
        String city = map.get("city");
        String districtName = map.get("district");
        System.out.println(province);
        System.out.println(city);
        System.out.println(districtName);
    }


}
